/**
 * 
 */
package com.dj.common.web.exception;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.validation.ObjectError;

@Component
public class CoreRestValidationFailHelper {

	private static final Logger logger = LoggerFactory.getLogger(CoreRestValidationFailHelper.class);

	public DjException getValidationException(String errorCode, List<ObjectError> allErrors) {
		Object[][] errorParams = new Object[allErrors.size()][2];
		for (int i = 0; i < allErrors.size(); i++) {
			ObjectError oe = allErrors.get(i);
			errorParams[i][0] = oe.getObjectName();
			errorParams[i][1] = oe.getDefaultMessage();
			logger.debug("Error for " + oe.getObjectName() + ": " + oe.getDefaultMessage());
		}

		return new InvalidRequestException(errorCode, errorParams);
	}
}
